package com.applink.sell_proof.fragment;

import java.util.Observable;
import java.util.Observer;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;
import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.R;
import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.database.table.CustomerSupportHelper;
import com.applink.sell_proof.model.CustomerSupport;


public class SupportContactFragment extends SherlockFragment implements Observer , OnClickListener{

	protected ViewGroup mLayout;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		if (mLayout == null) {
			mLayout = (ViewGroup) inflater.inflate(R.layout.custsvr_support, null, false);
		} else {
			if (mLayout.getParent() != null) {
				((ViewGroup) mLayout.getParent()).removeView(mLayout);
			}
		}
//		mLayout.findViewById(R.id.programhotline).setOnClickListener(this);
//		mLayout.findViewById(R.id.txtClientTech_hotline).setOnClickListener(this);
//		mLayout.findViewById(R.id.txtClientSale_hotline).setOnClickListener(this);
//		
//		mLayout.findViewById(R.id.program_email).setOnClickListener(this);
//		mLayout.findViewById(R.id.txtClientTech_email).setOnClickListener(this);
//		mLayout.findViewById(R.id.txtClientSale_email).setOnClickListener(this);
		
		SetCustomerInfo();
		
		return mLayout;
	}
	
	private void SetCustomerInfo(){
		
		CheckCustomerInfo();
		
		CustomerSupport entity = CustomerSupportHelper.GetCustomerInfo();
		
		BindDataToControl(entity);
	}
	
	private void BindDataToControl(CustomerSupport entity){
		
		if (entity == null){
			Log.e("BindDataToControl", "return value is null");
			return;
		}
			
		
//		TextView txtprogramHotline = (TextView)mLayout.findViewById(R.id.programhotline);
//		txtprogramHotline.setText(entity.getProgramhotline());
//		
//		TextView txtprogramEmail = (TextView)mLayout.findViewById(R.id.program_email);
//		txtprogramEmail.setText(entity.getProgramemail());
//		
//		TextView clientSaleEmail = (TextView)mLayout.findViewById(R.id.txtClientSale_email);
//		clientSaleEmail.setText(entity.getClient_sale_email());
//		
//		TextView clientSaleHotline = (TextView)mLayout.findViewById(R.id.txtClientSale_hotline);
//		clientSaleHotline.setText(entity.getClient_sale_hotline());
//		
//		TextView clienttechemail = (TextView)mLayout.findViewById(R.id.txtClientTech_email);
//		clienttechemail.setText(entity.getClient_tech_email());
//		
//		TextView clienttechhotline = (TextView)mLayout.findViewById(R.id.txtClientTech_hotline);
//		clienttechhotline.setText(entity.getClient_tech_hotline());
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//DataHelper.getInstance().addObserver(this);
	}


	private void CheckCustomerInfo(){
		SharedPreferences settings = EMarketing.getInstance().getSharedPreferences(Constant.SHAREDPREFERENCE_NAME, 0);
		String returnVal = settings.getString(Constant.PROGRAM_HOTLINE, "");
		
		if (returnVal == ""){
			//get from web service
			DataHelper.getInstance().getCustomerSupport();	
		}
	}
	

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
				if (data == null)
					return;
				DataResult result = (DataResult) data;
				if (result.getMethodName() == Methods.getCustomerProduct) {
					CustomerSupport entity = CustomerSupportHelper.GetCustomerInfo();
					if (entity != null) {
						BindDataToControl(entity);
					} else {
						Log.e(Constant.USER_SETTING_FRAGMENT, "no Support Contact");
					}
				}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
//		case R.id.programhotline:
//		case R.id.txtClientTech_hotline:
//		case R.id.txtClientSale_hotline:
//			Intent intent = new Intent();
//			intent.setAction(Intent.ACTION_DIAL);
//			String phone_number = String.format("tel:%s",((TextView)v).getText().toString());
//			Uri uri = Uri.parse(phone_number);
//			intent.setData(uri);
//			getActivity().startActivity(intent);
//			break;
//		case R.id.program_email:
//		case R.id.txtClientSale_email:
//		case R.id.txtClientTech_email:
//			intent = new Intent(Intent.ACTION_SEND);
//			intent.setType("text/plain");
//			intent.setType("vnd.android.cursor.dir/email");
//			intent.putExtra(Intent.EXTRA_EMAIL,
//					new String[] {((TextView)v).getText().toString() });
//			getActivity().startActivity(intent);
//			break;

		default:
			break;
		}
		
	}

}
